#include "page2.h"
#include <qdebug.h>
page2::page2(QWidget *parent)
	: QWidget(parent)
{
	this->hasReadFile = false;
	this->setWindowIcon(QIcon("Image\\运动会.svg"));
	QPixmap pixmap("Image\\光标.svg");
	//pixmap.scaled(40, 30, Qt::KeepAspectRatio, Qt::SmoothTransformation);
	pixmap = pixmap.scaled(30, 24, Qt::KeepAspectRatio);
	QCursor cursor(pixmap);
	this->setCursor(cursor);

	this->parent = dynamic_cast<SportMeeting*>(parent);
	ui.setupUi(this);
	ui.tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
	ui.tableWidget_2->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
	// 设置预设			75321
	predispose.resize(preNum);
	for (int i = 0; i < preNum; ++i) {
		predispose[i].resize(100, 0); // 将每个内层vector的大小改为n，初始值为0
	}
	predispose[0][0] = 7;
	predispose[0][1] = 5;
	predispose[0][2] = 3;
	predispose[0][3] = 2;
	predispose[0][4] = 1;
	// 532
	predispose[1][0] = 5;
	predispose[1][1] = 3;
	predispose[1][2] = 2;
	// 10 8 6 3 2 1
	predispose[2][0] = 10;
	predispose[2][1] = 8;
	predispose[2][2] = 6;
	predispose[2][3] = 3;
	predispose[2][4] = 2;
	predispose[2][5] = 1;
	// 连接信号与槽
	connect(ui.next_page, &QPushButton::clicked, this, &page2::next_page);
	connect(ui.preceding_page, &QPushButton::clicked, this, &page2::pre_page);
	connect(ui.configure, &QPushButton::clicked, this, &page2::configure);
	connect(ui.comboBox, &QComboBox::currentIndexChanged, this, &page2::chbox);
	connect(ui.preset, &QPushButton::clicked, this, &page2::saveAsPreset);
	connect(ui.delete_preset, &QPushButton::clicked, this, &page2::deletePreset);
	connect(ui.change_preset, &QPushButton::clicked, this, &page2::changePreset);

}
page2::page2(MyData* myData, QWidget* parent)
	: QWidget(parent)
{
	this->myData = myData;
	this->parent = dynamic_cast<SportMeeting*>(parent);
	ui.setupUi(this);
	// 连接信号与槽
	connect(ui.next_page, &QPushButton::clicked, this, &page2::next_page);
	connect(ui.preceding_page, &QPushButton::clicked, this, &page2::pre_page);
	connect(ui.configure, &QPushButton::clicked, this, &page2::configure);
}
page2::~page2()
{

}
void page2::loadData() {
	int column = this->myData->getTeamNum();
	ui.tableWidget->setColumnCount(column);
	ui.tableWidget_2->setColumnCount(column);
	int row = this->myData->getItemNum1()+ this->myData->getItemNum2();
	ui.tableWidget->setRowCount(row);
	QStringList labels;
	//vector<boyItem> boyItems = this->myData->getBoyItems();
	//vector<girlItem> girlItems = this->myData->getGirlItems();
	QVector<boyItem> boyItems = this->myData->getBoyItems();
	QVector<girlItem> girlItems = this->myData->getGirlItems();
	for (int i = 0; i < boyItems.size(); i++) {
		labels.append(boyItems[i].getName());
	}
	for (int i = 0; i < girlItems.size(); i++) {
		labels.append(girlItems[i].getName());
	}
	ui.tableWidget->setVerticalHeaderLabels(labels);
	if (hasReadFile) {
		for (int i = 0; i < boyItems.size(); i++)
		{
			int* val = boyItems[i].getVal();
			for (int j = 0; j < column; j++)
			{
				QString text = QString::number(val[j]);
				QTableWidgetItem* item = ui.tableWidget->item(i, j);
				if (item == nullptr) {
					item = new QTableWidgetItem(text);
					ui.tableWidget->setItem(i, j, item);
				}
				else {
					item->setText(text);
				}
				ui.tableWidget->item(i, j)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
			}
		}
		for (int i = boyItems.size(); i < row; i++)
		{
			int* val = girlItems[i-boyItems.size()].getVal();
			for (int j = 0; j < column; j++)
			{
				QString text = QString::number(val[j]);
				QTableWidgetItem* item = ui.tableWidget->item(i, j);
				if (item == nullptr) {
					item = new QTableWidgetItem(text);
					ui.tableWidget->setItem(i, j, item);
				}
				else {
					item->setText(text);
				}
				ui.tableWidget->item(i, j)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
			}
		}
		for (int j = 0; j < column; j++) {
			QTableWidgetItem* item = ui.tableWidget_2->item(0, j);
			if (item == nullptr) {
				item = new QTableWidgetItem();
				ui.tableWidget_2->setItem(0, j, item);
			}
			ui.tableWidget_2->item(0, j)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
		}
	}
	else {
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < column; j++)
			{
				QTableWidgetItem* item = ui.tableWidget->item(i, j);
				if (item == nullptr) {
					item = new QTableWidgetItem();
					ui.tableWidget->setItem(i, j, item);
				}
				ui.tableWidget->item(i, j)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
			}
		}
		for (int j = 0; j < column; j++) {
			QTableWidgetItem* item = ui.tableWidget_2->item(0, j);
			if (item == nullptr) {
				item = new QTableWidgetItem();
				ui.tableWidget_2->setItem(0, j, item);
			}
			ui.tableWidget_2->item(0, j)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
		}
	}
	
	
}
void page2::setData(MyData* data) {
	this->myData = data;
}
void page2::setHasReadFile(bool hasRead) {
	this->hasReadFile = hasRead;
}
// 槽函数
void page2::next_page() {
	this->parent->pageThree->setHasRead(hasReadFile);
	int column = this->myData->getTeamNum();
	int row1 = this->myData->getItemNum1();
	int row2 = this->myData->getItemNum2();
	//vector<boyItem>& boyItems = this->myData->getBoyItems();
	//vector<girlItem>& girlItems = this->myData->getGirlItems();
	QVector<boyItem>& boyItems = this->myData->getBoyItems();
	QVector<girlItem>& girlItems = this->myData->getGirlItems();
	int accept = -1;
	for (int i = 0; i < row1 + row2; i++) {
		for (int j = 0; j < column; j++) {
			QString text = ui.tableWidget->item(i, j)->text();
			bool isInt;
			int value = text.toInt(&isInt);
			if (!isInt) {
				accept = messageQuestion("警告", "存在非整数数值，将处理为0", QMessageBox::StandardButton::Yes, QMessageBox::StandardButton::No, this);
				break;
			}
		}
		if (accept != -1)	break;
	}
	if (accept == QMessageBox::Yes||accept == -1) {
		for (int i = 0; i < row1; i++) {
			for (int j = 0; j < column; j++) {
				int value = ui.tableWidget->item(i, j)->text().toInt();
				boyItems[i].setVal(j, value);
				//qDebug() << boyItems[i].getVal()[j];
			}
		}
		for (int i = row1; i < row1 + row2; i++) {
			for (int j = 0; j < column; j++) {
				int value = ui.tableWidget->item(i, j)->text().toInt();
				girlItems[i - row1].setVal(j, value);
			}
		}

		parent->pageThree->setData(this->myData);
		parent->pageThree->loadData();
		setPage(parent, parent->pageThree);
	}

}
void page2::pre_page() {
	setPage(parent,parent->pageOne);
}
void page2::configure() {
	QStringList texts;
	int column = this->myData->getTeamNum();
	int row = this->myData->getItemNum1() + this->myData->getItemNum2();
	bool isInt;
	int judge = -1;
	for (int i = 0; i < column; i++) {
		texts.append(ui.tableWidget_2->item(0, i)->text());
		if (texts[i].toInt(&isInt)==0||texts[i] == "" )	texts[i] = "0";
		if (!isInt&&judge==-1) {
			judge = messageQuestion("提示", "当前存在非整数值,将会设置为0,是否确定如此？", QMessageBox::StandardButton::Yes, QMessageBox::StandardButton::No, this);
		}
	}
	if (judge == -1 || judge == QMessageBox::StandardButton::Yes) {
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < column; j++) {
				ui.tableWidget->item(i, j)->setText(texts[j]);
			}
		}
	}
	if (judge == QMessageBox::StandardButton::Yes) {
		for (int j = 0; j < column; j++) {
			ui.tableWidget_2->item(0, j)->setText(texts[j]);
		}
	}
}
void page2::chbox() {
	int index = ui.comboBox->currentIndex();
	int num = this->myData->getTeamNum();
	QString text = ui.comboBox->currentText();
	if (text == "自定义") {
		ui.tableWidget_2->clearContents();
		for (int i = 0; i < num; i++) {
			QTableWidgetItem* item = new QTableWidgetItem();
			item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
			ui.tableWidget_2->setItem(0, i, item);
		}
	}
	else {
		for (int i = 0; i < num; i++) {
			QString text = QString::number(predispose[index][i]);
			QTableWidgetItem* item = new QTableWidgetItem(text);
			item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
			ui.tableWidget_2->setItem(0, i, item);
		}
	}
}

void page2::saveAsPreset() {
	// 1. 检查是否有空项 
	QString text = ui.comboBox->currentText();
	int column = this->myData->getTeamNum();
	int judge = -1;
	if (text == "自定义") {
		QString valText;
		int val;
		bool isInt;
		for (int j = 0; j < column; j++) {
			valText = ui.tableWidget_2->item(0, j)->text();
			val = valText.toInt(&isInt);
			if (!isInt) {
				judge = messageQuestion("提示", "当前存在非整数值,将会设置为0,是否确定如此？", QMessageBox::StandardButton::Yes, QMessageBox::StandardButton::No, this);
				break;
			}
		}
		if (judge == -1 || judge == QMessageBox::StandardButton::Yes) {
			int cnt = ui.comboBox->count();
			bool ok;
			QString newText;
			newText = QInputDialog::getText(this, "输入对话框", "请输入预设名：", QLineEdit::Normal,"",&ok);
			if (ok) {
				for (int j = 0; j < column; j++) {
					valText = ui.tableWidget_2->item(0, j)->text();
					val = valText.toInt();
					this->predispose[cnt-1][j] = val;
				}
				ui.comboBox->insertItem(cnt - 1, newText);
			}
		}
	}
	else {
		message("错误", "当前是已有预设！", QMessageBox::Icon::Critical, QMessageBox::Ok, this);
	}
}
void page2::changePreset() {
	QString text = ui.comboBox->currentText();
	int column = this->myData->getTeamNum();
	int index = ui.comboBox->currentIndex();
	int judge = -1;
	if (text == "自定义") {
		message("错误", "当前自定义！", QMessageBox::Icon::Critical, QMessageBox::Ok, this);
	}
	else {
		QString valText;
		int val;
		bool isInt;
		for (int j = 0; j < column; j++) {
			valText = ui.tableWidget_2->item(0, j)->text();
			val = valText.toInt(&isInt);
			if (!isInt) {
				judge = messageQuestion("提示", "当前存在非整数值,将会设置为0,是否确定如此？", QMessageBox::StandardButton::Yes, QMessageBox::StandardButton::No, this);
				break;
			}
		}
		if (judge == -1 || judge == QMessageBox::StandardButton::Yes) {
			for (int j = 0; j < column; j++) {
				valText = ui.tableWidget_2->item(0, j)->text();
				val = valText.toInt();
				this->predispose[index][j] = val;
			}
		}
	}
	
}
void page2::deletePreset() {
	QString text = ui.comboBox->currentText();
	if (text == "自定义") {
		message("错误", "当前自定义！", QMessageBox::Icon::Critical, QMessageBox::Ok, this);
	}
	else {
		int index = ui.comboBox->currentIndex();
		// 删除预设 comboBox
		ui.comboBox->removeItem(index);
		// 删除内容
		this->predispose.erase(predispose.begin() + index);
		// 设置为自定义
		int cnt = ui.comboBox->count();
		ui.comboBox->setCurrentIndex(cnt-1);
	}
}